package socketmvc.core.packets.biz;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import socketmvc.core.packets.basic.IPacket;

public abstract class AbstractChatBody<P,C> implements IMessage<C> {

    private static final long serialVersionUID = 5731474214655476286L;
    /**
     * 消息唯一id
     */
    protected String id;
    /**
     * 消息内容
     */
    protected C content;
    /**
     * 发送用户id;
     */
    protected P from;
    /**
     * 目标用户id;
     */
    protected P to;
    /**
     * 消息创建时间
     */
    protected Long createAt;
    /**
     * 消息类型;(如：0:text、1:file、2:image、3:voice、4:video、5:music、6:news)
     */
    protected Integer msgType;
    /**
     * 聊天类型;(1.私聊、2.公聊)
     */
    protected Integer chatType;

    /*
     * 消息发到哪个群组;
     * 可一次性设置发送给多个群组
     * 该字段只有 chatType ==2 的时候起作用
     */
    //private String groupId;

    /**
     * 附加字段，该字段主要是客户端用来传递一些自定义参数
     * 例如将用户的昵称传递到该字段中
     */
    protected JSONObject extras;

    public AbstractChatBody(){}

    public AbstractChatBody(String msgId ,
                            P from ,
                            P to ,
                            C content ,
                            Integer msgType ,
                            Integer chatType ,
                            Long createAt,
                            JSONObject extras){
        this.id = msgId;
        this.from = from ;
        this.to = to;
        this.content = content;
        this.msgType = msgType;
        this.chatType = chatType;
        if (createAt == null){
            createAt = System.currentTimeMillis();
        }
        this.createAt = createAt;
        this.extras = extras;
    }

    @Override
    public String getId() {
        return this.id;
    }


    public AbstractChatBody<P,C> setId(String id) {
        this.id = id;
        return this;
    }

    @JSONField(serialize = false)
    @Override
    public Integer getType() {
        return this.msgType;
    }
    public AbstractChatBody<P,C> setCreateAt(Long createTime) {
        this.createAt = createTime;
        return this;
    }
    @Override
    public Long getCreateAt() {
        return this.createAt;
    }

    public AbstractChatBody<P,C> setContent(C content) {
        this.content = content;
        return this;
    }
    @Override
    public C getContent() {
        return this.content;
    }

    public P getFrom() {
        return this.from;
    }

    public AbstractChatBody<P,C> setFrom(P from) {
        this.from = from;
        return this;
    }
    public P getTo() {
        return this.to;
    }

    public AbstractChatBody<P,C> setTo(P to) {
        this.to = to;
        return this;
    }

    public Integer getMsgType() {
        return this.msgType;
    }

    public AbstractChatBody<P,C> setMsgType(Integer msgType) {
        this.msgType = msgType;
        return this;
    }

    public Integer getChatType() {
        return chatType;
    }

    @JSONField(serialize = false)
    public ChatType getChatTypeEnum(){
        return ChatType.forNumber(chatType);
    }

    public AbstractChatBody<P,C> setChatType(Integer chatType) {
        this.chatType = chatType;
        return this;
    }

    public JSONObject getExtras() {
        return this.extras;
    }

    public AbstractChatBody<P,C> setExtras(JSONObject extras) {
        this.extras = extras;
        return this;
    }

    public IPacket toPacket(){
      return toPacket(null);
    }

    @Override
    public abstract IPacket toPacket(String cmd);

//    public static class Builder extends IMessage.Builder<ChatBody<?,?>,ChatBody.Builder>{
//        /**
//         * 来自user_id;
//         */
//        private Object from;
//        /**
//         * 目标user_id;
//         */
//        private Object to;
//        /**
//         * 消息类型;(如：0:text、1:image、2:voice、3:video、4:music、5:news)
//         */
//        private Integer msgType;
//        /**
//         * 聊天类型;(如公聊、私聊)
//         */
//        private Integer chatType;
//
//        private JSONObject extras;
//
//
//        public Builder(){};
//
//        public Builder from(Object from) {
//            this.from = from;
//            return this;
//        }
//        public Builder to(Object to) {
//            this.to = to;
//            return this;
//        }
//        public Builder msgType(Integer msgType) {
//            this.msgType = msgType;
//            return this;
//        }
//        public Builder chatType(Integer chatType) {
//            this.chatType = chatType;
//            return this;
//        }
//
//        @Override
//        protected Builder getThis() {
//            return this;
//        }
//
//        @Override
//        public ChatBody<?,?> build(){
//            return new ChatBody<>(
//                    this.id,
//                    this.from,
//                    this.to,
//                    this.msgType,
//                    this.chatType,
//                    this.content,
//                    this.createTime,
//                    this.extras);
//        }
//    }
}
